package com.yonyou.lxp.account.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.yonyou.lxp.account.BaseTwoActivity;
import com.yonyou.lxp.account.R;
import com.yonyou.lxp.account.entity.ExpenditureInfo;
import com.yonyou.lxp.account.utils.Constants;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.Date;

@ContentView(R.layout.activity_content)
public class ContentActivity extends BaseTwoActivity {

    @ViewInject(R.id.tv_content_name)
    private TextView tv_content_name;

    @ViewInject(R.id.tv_content_time)
    private TextView tv_content_time;

    @ViewInject(R.id.tv_content_money)
    private TextView tv_content_money;

    @ViewInject(R.id.tv_content_remarks)
    private TextView tv_content_remarks;

    private Intent intent;

    private ExpenditureInfo info;
    private boolean isUpData=false;//是否修改了

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_change:
                        if (info != null) {
                            if ("0".equals(info.getT_type())) {//支出
                                Intent it = new Intent(mContext, SpendingActivity.class);
                                it.putExtra("infoData", info);
                                startActivityForResult(it,10001);
                            } else {//收入
                                Intent it = new Intent(mContext, IncomeActivity.class);
                                it.putExtra("infoData", info);
                                startActivityForResult(it,10001);
                            }
                        }

                        break;
                }
                return true;
            }
        });
        intent = getIntent();
        info = intent.getParcelableExtra("data");
        init();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_content, menu);
        return true;
    }


    private void init() {
        tv_content_name.setText(info.getT_name());
        tv_content_time.setText(Constants.mFormatter.format(new Date(info.getE_time())));
        tv_content_money.setText((info.getT_type().equals("0") ? "-" : "+") + info.getE_money());
        tv_content_remarks.setText(info.getE_remarks());
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            switch (requestCode) {
                case 10001:
                    isUpData=true;
                    Intent it=new Intent();
                    setResult(10003,it);
                    info = data.getParcelableExtra("infoData");
                    init();
                    break;
            }
        }
    }
}
